package flc.ast.fragment.edit;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.ModuleConfig;

import java.util.Random;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.databinding.FragmentRepairBinding;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RepairFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RepairFragment extends BaseEditFragment<FragmentRepairBinding> {

    public static final int INDEX = 10;
    private Bitmap mImgBitmap, mSaveBmp;
    private boolean isRepair;

    public static RepairFragment newInstance() {
        RepairFragment fragment = new RepairFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mImgEditActivity.mode = 10;

        mDataBinding.ivRepair.setImageResource(R.drawable.awxf);
        mDataBinding.tvRepair.setTextColor(Color.parseColor("#858585"));

        mImgBitmap = mImgEditActivity.getMainBit();
        mSaveBmp = mImgBitmap;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_repair;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivApply.setOnClickListener(this);
        mDataBinding.llRepair.setOnClickListener(this);

        onShow();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCancel://取消
                backToMain();
                break;
            case R.id.ivApply://确定
                applyRepairImage();
                break;
            case R.id.llRepair://一键修复
                if (isRepair) {
                    isRepair = false;
                    mSaveBmp = mImgBitmap;
                    mImgEditActivity.mImgView.setImageBitmap(mSaveBmp);
                    mDataBinding.ivRepair.setImageResource(R.drawable.awxf);
                    mDataBinding.tvRepair.setTextColor(Color.parseColor("#858585"));
                } else {
                    isRepair = true;
                    repair();
                    mDataBinding.ivRepair.setImageResource(R.drawable.ayxf2);
                    mDataBinding.tvRepair.setTextColor(Color.parseColor("#2B6AFF"));
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void repair() {
        showDialog(getString(com.stark.common.res.R.string.handling));
        ApiManager.visionAiApi().repairOldPhoto(this, mImgBitmap, new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                if (result != null) {
                    mSaveBmp = result;
                    mImgEditActivity.mImgView.setImageBitmap(mSaveBmp);
                }
                dismissDialog();
            }
        });
    }

    /**
     * 保存图片
     */
    private void applyRepairImage() {
        mImgEditActivity.changeMainBitmap(mSaveBmp, true);
        backToMain();
    }

}